Public Class LookupDataInputDialog
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
    Protected WithEvents lblErr As System.Web.UI.WebControls.Label
    Protected WithEvents btnReset As System.Web.UI.HtmlControls.HtmlInputButton

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Protected WithEvents LKDInputControl As LookupDataInputControl

    Private Const LKD_OBJECT_SESSION_NAME As String = "MIMS_WRS.Web.LookUpDataInput.PageBO"

    Protected Property PageBO() As CLookupData
        Get
            Return CType(Session(Me.LKD_OBJECT_SESSION_NAME), CLookupData)
        End Get
        Set(ByVal Value As CLookupData)
            Session(Me.LKD_OBJECT_SESSION_NAME) = Value
        End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Me.IsPostBack Then

            Dim lkID As Integer
            Dim lkdID As Integer

            ' the look up ID must be supply
            lkID = CInt(Request("id"))

            Try
                lkdID = CInt(Request("lkdID"))
            Catch ex As Exception
                lkdID = 0
            End Try

            Dim obj As New CLookupData

            If lkdID > 0 Then
                Try
                    obj.Load(lkdID)
                Catch ex As Exception
                    Throw New ApplicationException("Failed to load look up data", ex)
                End Try

                If obj.LookupID <> lkID Then
                    Throw New ApplicationException("Look up data invalid. Look up ID not match.)")
                End If
            Else
                obj.Init()
                btnReset.Visible = False
            End If

            obj.LookupID = lkID

            Me.PageBO = obj
            Me.LKDInputControl.PageBOSessionName = Me.LKD_OBJECT_SESSION_NAME
        End If
    End Sub

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Try
            PageBO.Save()
            Me.SetReturnValue()
        Catch ex As DataEntryException
            'Me.lblErr.Text = ex.Message
            Dim idx As Integer = ex.Message.LastIndexOf(";"c)
            Page.RegisterStartupScript("ValueExists", Me.ValueExistsScript(ex.Message.Substring(0, idx), ex.Message.Substring(idx + 1, ex.Message.Length - idx - 1)))
        Catch ex As Exception
            Me.lblErr.Text = ex.Message
        End Try
    End Sub

    Private Sub SetReturnValue()
        Dim s As String = ""
        s += "var obj= new Object();"
        s += String.Format("obj.ID = '{0}';", PageBO.ID)
        s += String.Format("obj.Value1 = '{0}';", PageBO.Value1)
        s += String.Format("obj.Value2 = '{0}';", PageBO.Value2)
        s += String.Format("obj.Value3 = '{0}';", PageBO.Value3)
        s += "window.returnValue = obj;"
        s += "window.close();"
        Response.Write(String.Format("<script>{0}</script", s))
    End Sub

    Private Function ValueExistsScript(ByVal errMsg As String, ByVal lkdID As String) As String
        Dim s As String = "<script>"
        s += "var obj= new Object();"
        s += String.Format("obj.ID = '{0}';", lkdID)
        s += String.Format("obj.Value1 = '{0}';", PageBO.Value1)
        s += String.Format("obj.Value2 = '{0}';", PageBO.Value2)
        s += String.Format("obj.Value3 = '{0}';", PageBO.Value3)
        s += String.Format("if (confirm('{0}\nDo you want to use this value or re-enter a new value?\nPress [OK] to use this value\nPress [Cancel] to enter a new value'))", errMsg)
        s += "{"
        s += "window.returnValue = obj;"
        s += "window.close();"
        s += "}"
        s += "</script>"
        Return s
    End Function
End Class
